
package com.rt.appauthorize;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.FilterChain;
import javax.servlet.ServletResponse;
import javax.servlet.ServletRequest;
import javax.servlet.FilterConfig;
import javax.servlet.Filter;

@Deprecated
public class AppAuthFilter implements Filter {
    private static Logger log = LoggerFactory.getLogger(AppAuthFilter.class);

    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        String contextPath = request.getContextPath();
        String authUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/rtAuth/index";
        String requestURI = request.getRequestURI();
        response.setCharacterEncoding("utf-8");
        if (contextPath == null) {
            contextPath = "";
        }
        String path = requestURI.substring(contextPath.length());
        if (path.startsWith("/rtAuth/index")) {
            chain.doFilter(request, response);
            return;
        }
        boolean verifyAuth = false;
        if (!verifyAuth) {
            System.out.println("权限过滤器拦截");
            log.info("权限过滤器拦截");
            response.sendRedirect(authUrl);
        } else {
            System.out.println("权限过滤器放行");
            log.info("权限过滤器放行");
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
    }
}
